﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Fc
{
    public partial class JinDu_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;

            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDID"] != null)
                {
                    int did = (int)Session["OperatorDID"];
                    if (did != 0)
                    {
                        this.FenChang.Text = did.ToString();
                        this.FenChang.Enabled = false;
                        if (did == 1)
                        {
                            this.ChanPin.Text = "2";
                            this.ChanPin.Enabled = false;
                        }
                        else if (did == 5)
                        {
                            this.ChanPin.Text = "1";
                            this.ChanPin.Enabled = false;
                            this.RadioButton1.Visible = true;
                            this.RadioButton2.Visible = true;
                            this.CheSi3.Enabled = false;
                            this.CheSi4.Enabled = false;
                            this.CheSi5.Enabled = false;
                            this.CheSi6.Enabled = false;
                        }
                    }
                }
                
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    this.Label1.Text = "修改";
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText ="select * from [生产进度] where [ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {

                        this.ViewState["BillID"] = BillID;

                        this.RiQi.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                        this.FenChang.Text = ((int)Reader["分厂ID"]).ToString();
                        if ((int)Reader["生产线"] == 2)
                        {
                            this.RadioButton2.Checked = true;
                            RadioButton2_CheckedChanged(null, null);
                        }

                        this.FenChang.Text = ((int)Reader["分厂ID"]).ToString();

                        this.GongDuan.Text = ((int)Reader["工段"]).ToString();
                        this.ChanPin.Text = ((int)Reader["产品类别ID"]).ToString();

                        this.JiHuaZhi.Text = ((int)Reader["计划支数"]).ToString();
                        this.JiHuaZhong.Text = ((double)Reader["计划重量"]).ToString();
                        this.TouRuZhi.Text = ((int)Reader["投入支数"]).ToString();
                        this.TouRuZhong.Text = ((double)Reader["投入重量"]).ToString();

                        this.CheSi1.Text = ((int)Reader["车丝1"]).ToString();
                        this.CheSi2.Text = ((int)Reader["车丝2"]).ToString();
                        this.CheSi3.Text = ((int)Reader["车丝3"]).ToString();
                        this.CheSi4.Text = ((int)Reader["车丝4"]).ToString();
                        this.CheSi5.Text = ((int)Reader["车丝5"]).ToString();
                        this.CheSi6.Text = ((int)Reader["车丝6"]).ToString();

                        this.JiJin1.Text = ((int)Reader["机紧1"]).ToString();
                        this.JiJin2.Text = ((int)Reader["机紧2"]).ToString();

                        this.JingShuiYa.Text = ((int)Reader["静水压"]).ToString();
                        this.CCZhi.Text = ((int)Reader["称测支数"]).ToString();
                        this.CCChang.Text = ((double)Reader["称测长度"]).ToString();
                        this.CCZhong.Text = ((double)Reader["称测重量"]).ToString();

                        this.BZZhi.Text = ((int)Reader["包装支数"]).ToString();
                        this.BZKun.Text = ((int)Reader["包装捆数"]).ToString();
                        this.BZChang.Text = ((double)Reader["包装长度"]).ToString();
                        this.BZZhong.Text = ((double)Reader["包装重量"]).ToString();

                        this.RKZhi.Text = ((int)Reader["入库支数"]).ToString();
                        this.RKKun.Text = ((int)Reader["入库捆数"]).ToString();
                        this.RKChang.Text = ((double)Reader["入库长度"]).ToString();
                        this.RKZhong.Text = ((double)Reader["入库重量"]).ToString();

                        this.WRKZhi.Text = ((int)Reader["未入库支数"]).ToString();
                        this.WRKZhong.Text = ((double)Reader["未入库重量"]).ToString();
                    }

                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
            }

            if (this.RiQi.Text=="")
            {
                if (DateTime.Now.Hour > 12)
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
                else
                {
                    RiQi.Text = DateTime.Today.AddDays(-1).ToString("yyyy年M月d日");
                }
            }
        }


        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            DateTime riqi;
            if (!DateTime.TryParse(this.RiQi.Text, out riqi))
            {
                this.Message.Text = "·输入的日期有误";
                return;
            }

            int jihuazhi;
            if (!int.TryParse(this.JiHuaZhi.Text, out jihuazhi))
            {
                this.Message.Text = "·钢管计划支数输入有误";
                return;
            }
            double jihuazhong;
            if (!double.TryParse(this.JiHuaZhong.Text, out jihuazhong))
            {
                this.Message.Text = "·钢管计划重量输入有误";
                return;
            }

            int touruzhi;
            if (!int.TryParse(this.TouRuZhi.Text, out touruzhi))
            {
                this.Message.Text = "·钢管投入支数输入有误";
                return;
            }
            double touruzhong;
            if (!double.TryParse(this.TouRuZhong.Text, out touruzhong))
            {
                this.Message.Text = "·钢管投入重量输入有误";
                return;
            }

            int[] chesi=new int[6];
            for (int i = 0; i < 6; i++)
            {
                if (!int.TryParse(((TextBox)this.FindControl("CheSi"+(i+1))).Text, out chesi[i]))
                {
                    this.Message.Text = "·"+(i+1)+"#车丝数量输入有误";
                    return;
                }
            }
            int jijin1;
            if (!int.TryParse(this.JiJin1.Text, out jijin1))
            {
                this.Message.Text = "·机紧 自产支数输入有误";
                return;
            }
            int jijin2;
            if (!int.TryParse(this.JiJin2.Text, out jijin2))
            {
                this.Message.Text = "·机紧 外购支数输入有误";
                return;
            }

            int jinshuiya;
            if (!int.TryParse(this.JingShuiYa.Text, out jinshuiya))
            {
                this.Message.Text = "·静水压合格支数输入有误";
                return;
            }

            int cczhi;
            if (!int.TryParse(this.CCZhi.Text, out cczhi))
            {
                this.Message.Text = "·称重测长支数输入有误";
                return;
            }
            double ccchang;
            if (!double.TryParse(this.CCChang.Text, out ccchang))
            {
                this.Message.Text = "·称重测长长度输入有误";
                return;
            }
            double cczhong;
            if (!double.TryParse(this.CCZhong.Text, out cczhong))
            {
                this.Message.Text = "·称重测长重量输入有误";
                return;
            }

            int bzzhi;
            if (!int.TryParse(this.BZZhi.Text, out bzzhi))
            {
                this.Message.Text = "·成品包装支数输入有误";
                return;
            }

            int bzkun;
            if (!int.TryParse(this.BZKun.Text, out bzkun))
            {
                this.Message.Text = "·成品包装捆数输入有误";
                return;
            }

            double bzchang;

            if (!double.TryParse(this.BZChang.Text, out bzchang))
            {
                this.Message.Text = "·成品包装长度输入有误";
                return;
            }

            double bzzhong;
            if (!double.TryParse(this.BZZhong.Text, out bzzhong))
            {
                this.Message.Text = "·成品包装重量输入有误";
                return;
            }


            int rkzhi;
            if (!int.TryParse(this.RKZhi.Text, out rkzhi))
            {
                this.Message.Text = "·成品入库支数输入有误";
                return;
            }

            int rkkun;
            if (!int.TryParse(this.RKKun.Text, out rkkun))
            {
                this.Message.Text = "·成品入库捆数输入有误";
                return;
            }

            double rkchang;

            if (!double.TryParse(this.RKChang.Text, out rkchang))
            {
                this.Message.Text = "·成品入库长度输入有误";
                return;
            }

            double rkzhong;
            if (!double.TryParse(this.RKZhong.Text, out rkzhong))
            {
                this.Message.Text = "·成品入库重量输入有误";
                return;
            }

            int wrkzhi;
            if (!int.TryParse(this.WRKZhi.Text, out wrkzhi))
            {
                this.Message.Text = "·未成品入库支数输入有误";
                return;
            }


            double wrkzhong;
            if (!double.TryParse(this.WRKZhong.Text, out wrkzhong))
            {
                this.Message.Text = "·未成品入库重量输入有误";
                return;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            Cmd.CommandText = "JinDu_In";
            Cmd.CommandType = CommandType.StoredProcedure;
            if (this.ViewState["BillID"] == null)
            {
                this.ViewState["BillID"] = -1;
            }

            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "未知用户";
            }
            int scx = 0;

            if ((object)Session["OperatorDID"] != null)
            {
                int did = (int)Session["OperatorDID"];
                if (did ==5)
                {
                    if (this.RadioButton1.Checked)
                    {
                        scx = 1;
                    }
                    else
                    {
                        scx = 2;
                    }
                }
            }


            Cmd.Parameters.Add(new SqlParameter("@分厂ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.FenChang.Text));
            Cmd.Parameters.Add(new SqlParameter("@生产线", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, scx));

            Cmd.Parameters.Add(new SqlParameter("@日期", SqlDbType.SmallDateTime, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, riqi));
            Cmd.Parameters.Add(new SqlParameter("@产品类别ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ChanPin.Text));
            Cmd.Parameters.Add(new SqlParameter("@工段", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.GongDuan.Text));

            Cmd.Parameters.Add(new SqlParameter("@计划支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jihuazhi));
            Cmd.Parameters.Add(new SqlParameter("@计划重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jihuazhong));
            Cmd.Parameters.Add(new SqlParameter("@投入支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, touruzhi));
            Cmd.Parameters.Add(new SqlParameter("@投入重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, touruzhong));
            Cmd.Parameters.Add(new SqlParameter("@车丝1", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[0]));
            Cmd.Parameters.Add(new SqlParameter("@车丝2", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[1]));
            Cmd.Parameters.Add(new SqlParameter("@车丝3", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[2]));
            Cmd.Parameters.Add(new SqlParameter("@车丝4", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[3]));
            Cmd.Parameters.Add(new SqlParameter("@车丝5", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[4]));
            Cmd.Parameters.Add(new SqlParameter("@车丝6", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, chesi[5]));
            Cmd.Parameters.Add(new SqlParameter("@机紧1", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jijin1));
            Cmd.Parameters.Add(new SqlParameter("@机紧2", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jijin2));
            Cmd.Parameters.Add(new SqlParameter("@静水压", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, jinshuiya));
            Cmd.Parameters.Add(new SqlParameter("@称测支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, cczhi));
            Cmd.Parameters.Add(new SqlParameter("@称测长度", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, ccchang));
            Cmd.Parameters.Add(new SqlParameter("@称测重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, cczhong));
            Cmd.Parameters.Add(new SqlParameter("@包装支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, bzzhi));
            Cmd.Parameters.Add(new SqlParameter("@包装捆数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, bzkun));
            Cmd.Parameters.Add(new SqlParameter("@包装长度", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, bzchang));
            Cmd.Parameters.Add(new SqlParameter("@包装重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, bzzhong));
            Cmd.Parameters.Add(new SqlParameter("@入库支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, rkzhi));
            Cmd.Parameters.Add(new SqlParameter("@入库捆数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, rkkun));
            Cmd.Parameters.Add(new SqlParameter("@入库长度", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, rkchang));
            Cmd.Parameters.Add(new SqlParameter("@入库重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, rkzhong));
            Cmd.Parameters.Add(new SqlParameter("@未入库支数", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, wrkzhi));
            Cmd.Parameters.Add(new SqlParameter("@未入库重量", SqlDbType.Float, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, wrkzhong));

            Cmd.Parameters.Add(new SqlParameter("@填报人", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, op));
            Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.InputOutput, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));

            Cmd.ExecuteNonQuery();

            this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
            int BillID = (int)this.ViewState["BillID"];
            if (BillID > 0)
            {
                this.Button_New.Visible = true;
                this.Message.Text = "·保存成功";
            }
            else
            {
                this.Message.Text = "·进度已经存在,请更改进度日期、班次、产品";
            }

            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();

        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("JinDu_In.aspx"); 
        }


        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.CheSi1.Enabled = true;
            this.CheSi2.Enabled = true;
            this.CheSi3.Enabled = false;
            this.CheSi4.Enabled = false;
        }

        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.CheSi3.Enabled = true;
            this.CheSi4.Enabled = true;
            this.CheSi1.Enabled = false;
            this.CheSi2.Enabled = false;
        }

    }
}
